
myApp.controller('command.index',['$scope','$rootScope','obdService',function($scope,$rootScope,obdService){
    $scope.accountTable = {};

    $scope.$watch('$viewContentLoaded', function (event, msg) {
        initialObdTable();
    });

    function initialObdTable() {
        if ($('#account-table').hasClass('dataTable')) {
            $scope.accountTable.clear();
            $scope.accountTable.rows.add(data).draw();
        } else {
            $scope.accountTable = $('#obd-table').DataTable({
                "dom": '<"row"<"col-md-6"B><"col-md-1"><"col-md-5"f>><tip>',
                "buttons": [
                    {
                        text: 'Upload',
                        className: 'btn  btn-sm btn-info',
                        action: function (e, dt, node, config) {
                            $scope.toUpload();
                        }
                    }
                ],
                "paging": true,
                "pagingType": "full_numbers",
                "processing": true,
                "serverSide": true,
                "ajax": {
                    "url": config.backend_url + '/web/obd',
                    "type": "POST",
                    "dataType": 'json',
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("Authorization", "Bearer " + $rootScope.accessToken);
                        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
                    },
                },
                "columns": [
                    {"data": "serialsNumber"},
                    {"data": "productModel"},
                    {"data": "bindTime"},
                    {"data": "bindAccount"},
                    {"data": "status"},
                    {"data": "remark"}
                ],
                "lengthChange": false,
                "searching": true,
                "retrieve": true,
                "order": [[1, 'asc']],//排序
                "columnDefs": [
                    {
                        "targets": [4],//设置表格第7列的数据显示
                        "render": function (data, type, full, meta) {
                            var data = '' ;
                            if ( data == -1 ) data = '<span class="label label-danger">Repair</span>';
                            if ( data == 0 ) data = '<span class="label label-warning">UnBind</span>';
                            if ( data == 1 ) data = '<span class="label label-success">Bind</span>';
                            return data ;
                        }
                    }//,
                    // {
                    //     "targets": [2],//设置表格第5列的数据显示
                    //     "render": function (data, type, full, meta) {
                    //         return data ? '<span class="label label-success">Yes</span>' : '<span class="label label-danger">No</span>';
                    //     }
                    // }
                ],
                "ordering": false,
                "info": false,
                "autoWidth": false,
                "iDisplayLength": 9,
                "oLanguage": {//界面语言...
                    "oPaginate": {
                        "sPrevious": "&laquo;",
                        "sNext": "&raquo;"
                    },
                    "sSearch": "<i class='fa fa-search'></i>",
                    "sSearchPlaceholder": "Search",
                    "sZeroRecords": "没有检索到符合条件的数据",
                    'emptyTable': '无相应数据',
                    'loadingRecords': '加载中...',
                    'processing': '查询中...'
                }
            })
        }
    }
    
    $scope.toUpload = function () {
        $('#myModal').modal();
    }


    $("#uploadfile").fileinput({
        language: 'en', //设置语言
        uploadUrl: "/upload/new-obd", //上传的地址
        allowedFileExtensions: ['xlsx'],//接收的文件后缀
        uploadAsync: true, //默认异步上传
        showUpload: true, //是否显示上传按钮

        showRemove : true, //显示移除按钮
        showPreview : true, //是否显示预览
        showCaption: false,//是否显示标题
        browseClass: "btn btn-primary", //按钮样式
        dropZoneEnabled: false,//是否显示拖拽区域
        maxFileCount: 1, //表示允许同时上传的最大文件个数
        enctype: 'multipart/form-data',
        validateInitialCount:true
    });
//异步上传返回结果处理
    $("#uploadfile").on("fileuploaded", function (event, data, previewId, index) {
        var response = data.response;
        if ( response.success ){
            swal("上传成功!", response.msg, "success");
        }else{
            swal("上传失败", response.msg, "error");
        }
        $('#obd-table').DataTable().draw(true);
    });
//上传前

    $('#uploadfile').on('filepreupload', function(event, data, previewId, index) {
        var form = data.form, files = data.files, extra = data.extra,
            response = data.response, reader = data.reader;
    });


    }]);
